﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Ultima.Xna.Input;

namespace Ultima.Xna.SceneManagement
{
    public class WarningScene : BaseScene
    {
        TimeSpan _displayTime = TimeSpan.FromSeconds(2.5);
        float _timeElapsed;
        bool _continue;
        bool _inTransition;
        Texture2D _background;
        SpriteFont _font;

        public WarningScene(Game game)
            : base(game)
        {

        }

        public void OnMouseUp(object sender, MouseEventArgs e)
        {
            _continue = true;
        }

        public override void Intitialize()
        {
            base.Intitialize();

            OnProgressUpdate(this, new ProgressUpdateEventArgs(0, 2));
            _background = Content.Load<Texture2D>("Images\\WarningScene\\background");
            OnProgressUpdate(this, new ProgressUpdateEventArgs(1, 2));
            _font = Content.Load<SpriteFont>("Fonts\\WarningScene");
            OnProgressUpdate(this, new ProgressUpdateEventArgs(2, 2));

            Input.KeyUp += new EventHandler<KeyboardEventArgs>(OnKeyUp);
            Input.MouseUp += new EventHandler<MouseEventArgs>(OnMouseUp);
        }

        public override void Dispose()
        {
            Input.KeyUp -= new EventHandler<KeyboardEventArgs>(OnKeyUp);
            Input.MouseUp -= new EventHandler<MouseEventArgs>(OnMouseUp);

            base.Dispose();
        }

        private void OnKeyUp(object sender, KeyboardEventArgs e)
        {
            if (e.ContainsKeyPressed(Keys.Escape))
            {
                Game.Exit();
            }

            _continue = true;
        }

        public override void Update(GameTime gameTime)
        {
            _timeElapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (!_inTransition && _continue && _timeElapsed >= _displayTime.TotalSeconds)
            {
                _inTransition = true;
                SceneManager.CurrentScene = new LoadingScene(Game, new LoginScene(Game));
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            SpriteBatch.Begin();

            PresentationParameters pp = SpriteBatch.GraphicsDevice.PresentationParameters;

            int centerX = pp.BackBufferWidth / 2;
            int centerY = pp.BackBufferHeight / 2;

            SpriteBatch.Draw(
                _background, 
                new Vector2(centerX, centerY), 
                null, 
                Color.White,
                0,
                new Vector2(_background.Width / 2, _background.Height / 2), 
                1,
                SpriteEffects.None,
                0);

            string warningHeader = "Warning";
            string warningContent = "You have enabled custom scripts.  Please be aware that these\n " +
                                    "scripts are not written and/or endorsed by the Ultima XNA team.\n  " +
                                    "Please be aware that there is no way to be sure if the scripts\n " +
                                    "are intended to cause malicious acts and steal personal information \n" +
                                    "from your computer.  Please use these scripts cautiously and\n " +
                                    "at your own risk.";
            string pressAnyKeyOrExit = "Press any key to continue or Esc to exit!";

            SpriteBatch.DrawString(_font, warningHeader, new Vector2(centerX - _font.MeasureString(warningHeader).X / 2f, centerY - 150), Color.White);
            SpriteBatch.DrawString(_font, warningContent, new Vector2(centerX - _font.MeasureString(warningContent).X / 2f, centerY - 100), Color.White);
            SpriteBatch.DrawString(_font, pressAnyKeyOrExit, new Vector2(centerX - _font.MeasureString(pressAnyKeyOrExit).X / 2f, centerY + 100), Color.White);

            SpriteBatch.End();
        }
    }
}
